// 利用 Vite 的功能自动生成路由
export default function autoGenerateRoutes() {
  const pages = import.meta.glob<boolean, string, { default: React.ComponentType }>('@pages/*.tsx')

  const routes = Object.keys(pages).map(originPath => {
    const matches = originPath.match(/([^/]+)\.tsx$/) || []
    const name = matches[1]
    const path = `/${name.toLowerCase()}`;
    const Element = lazy(pages[originPath])

    return {
      path,
      name,
      element: <Element />
    };
  });

  // 特殊处理
  routes.map(route => {
    if (route.path === "/index") { route.path = "/" }
  })

  return routes
}
